package com.ybkj.o2o.sales.controller.res;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.sales.common.DataGrid;
import com.ybkj.o2o.sales.common.transport.Error;
import com.ybkj.o2o.sales.common.transport.Result;
import com.ybkj.o2o.sales.entity.CouponRule;
import com.ybkj.o2o.sales.manager.CouponRuleManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@SuppressWarnings("all")
@RestController
@RequestMapping(value = "api/couponrules")
public class CouponRuleRes {
  @Autowired
  private CouponRuleManager couponRuleManager;

  /**
   * 生成优惠券规则.
   * 
   * @param title 优惠券简介
   * @param code 编码
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param type 优惠券类型
   * @param discount 打折优惠券
   * @param money 抵扣优惠券
   * @param number 优惠券数量 (最多发放多少张)
   * @param eachLimit 每人限领：0为不限制
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "create", method = RequestMethod.POST)
  public ResponseEntity<Object> create(@RequestParam("title") String title,
      @RequestParam("code") String code, 
      @RequestParam("type") Integer type,
      @RequestParam("couponType") String couponType, 
      @RequestParam(required = false,value ="couponTypeId") Long couponTypeId, 
      @RequestParam(required = false,value = "number") Integer number,
      @RequestParam(required = false, value = "eachLimit") Integer eachLimit,
      @RequestParam("start") Long startTime,
      @RequestParam(required = false, value = "end") Long endTime,
      @RequestParam("companyId") Long companyId,
      @RequestParam(required = false, value = "discount") Double discount, 
      @RequestParam(required = false, value = "money") BigDecimal money, 
      @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(title), "title不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(code), "code不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(startTime != null, "startTime不能为空");
    Preconditions.checkArgument(companyId != null, "companyId不能为空");
    Preconditions.checkArgument(type != null, "type不能为空");
    Preconditions.checkArgument(eachLimit != null, "eachLimit不能为空");
    try {
      Date start = new Date(startTime);
      Date end = null;
      if (endTime != null) {
        end = new Date(endTime);
      }
      CouponRule cr =
          couponRuleManager.insert(
              title, code, start, end, companyId, type, couponType, couponTypeId,
              discount, money, number, eachLimit, appKey);

      Result<CouponRule> result = new Result<CouponRule>(0, "成功", cr);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 分页查询出优惠券规则信息.
   * 
   * @param companyId 公司id
   * @param page 页数
   * @param rows 每页数量
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "findAll", method = RequestMethod.GET)
  public ResponseEntity<Object> findAll(
      @RequestParam(required = false, value = "companyId") Long companyId, @RequestParam(
          required = false, value = "page") Integer page, @RequestParam(required = false,
          value = "rows") Integer rows, @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      DataGrid<CouponRule> couponRule = couponRuleManager.queryAll(companyId, page, rows, appKey);
      Result<DataGrid<CouponRule>> result = new Result<DataGrid<CouponRule>>(0, "成功", couponRule);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

  /**
   * 根据companyId查询出有效的优惠券规则信息.
   * 
   * @param companyId 公司id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "findValid", method = RequestMethod.GET)
  public ResponseEntity<Object> findValid(
      @RequestParam(required = false, value = "companyId") Long companyId,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      List<CouponRule> list = couponRuleManager.queryValid(companyId, appKey);
      Result<List<CouponRule>> result = new Result<List<CouponRule>>(0, "成功", list);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }


  /**
   * 根据ID查询出优惠券规则信息.
   * 
   * @param id 优惠券规则id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "findById", method = RequestMethod.GET)
  public ResponseEntity<Object> findById(@RequestParam(required = false, value = "id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {

      CouponRule couponRule = couponRuleManager.findByIdAndAppKey(id, appKey);
      Result<CouponRule> result = new Result<CouponRule>(0, "成功", couponRule);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 删除优惠券.
   * 
   * @param id 优惠券规则id
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "delete", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(@RequestParam("id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {

      couponRuleManager.delete(id, appKey);
      Result<CouponRule> result = new Result<CouponRule>(0, "成功", null);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 失效优惠券.
   * 
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "lapsed", method = RequestMethod.PUT)
  public ResponseEntity<Object> lapsed(@RequestParam("id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    try {

      couponRuleManager.update(id, appKey);
      Result<CouponRule> result = new Result<CouponRule>(0, "成功", null);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }
}
